package isp.apps.example.shoppingadmin.action;

import com.opensymphony.xwork.ActionSupport;

import isp.apps.example.shoppingcart.domain.ShoppingCartValidator;
import isp.apps.example.shoppingcart.domain.CompletedOrder;
import isp.apps.example.shoppingcart.domain.OrderStatus;
import isp.apps.example.shoppingcart.persistence.CompletedOrderDirectory;
import isp.apps.example.shoppingcart.persistence.CompletedOrderRepository;
import isp.apps.example.shoppingcart.persistence.OrderStatusDirectory;
import isp.apps.example.shoppingcart.persistence.OrderStatusRepository;
import isp.apps.example.shoppingcart.persistence.factory.CompletedOrderPersistenceFactory;
import isp.apps.example.shoppingcart.persistence.factory.OrderStatusPersistenceFactory;

public class OrderStatusUpdateAction extends ActionSupport
{

  private String newStatusId;
  private String updateOrderId;
  
  private CompletedOrderDirectory orderDirectory;
  private OrderStatusDirectory statusDirectory;
  
  private CompletedOrderRepository orderRepository;
  
  private CompletedOrder updatedOrder;
  private OrderStatus newStatus;
  
  public OrderStatusUpdateAction()
  {
  }
  
  
  
  
  public String execute()
  {
    validate();
    if (hasActionErrors())
      return SUCCESS;
      
    updateOrderStatus();
    return SUCCESS;
  }
  
  private void updateOrderStatus()
  {
    orderRepository = CompletedOrderPersistenceFactory.getCompletedOrderRepository();
    updatedOrder.setOrderStatusId(newStatus.getId());
    orderRepository.saveCompletedOrder(updatedOrder);
  }
  
  public void validate()
  {
    checkForExistenceOfOrderAndStatus();
    if (hasActionErrors())
      return;
      
    ShoppingCartValidator scv = new ShoppingCartValidator();
    if ((updateOrderId = scv.checkNumber(updateOrderId)) == null)
    {
      addActionError("Invalid order id '" + updateOrderId + "' is not a positive integer");
      return;
    }
    
    if ((newStatusId = scv.checkNumber(newStatusId)) == null)
    {
      addActionError("Status id '" + newStatusId + "' is not a positive integer");
      return;
    }
  }
    
  private void checkForExistenceOfOrderAndStatus()
  {
    orderDirectory = CompletedOrderPersistenceFactory.getCompletedOrderDirectory();
    statusDirectory = OrderStatusPersistenceFactory.getOrderStatusDirectory();
    
    updatedOrder = orderDirectory.getOrderById(new Integer(updateOrderId));
    if (updatedOrder == null)
    {
      addActionError("Unable to find an order with id " + updateOrderId);
      return;
    }
    
    newStatus = statusDirectory.getStatusById(new Integer(newStatusId));
    if (newStatus == null)
    {
      addActionError("Unable to find an status with id " + newStatusId);
      return;
    }
    
    //newStatus  newStatusId;
    //updatedOrder  updateOrderId;
  }

  //accessors

  public void setUpdateOrderId(String updateOrderId)
  {
    this.updateOrderId = updateOrderId;
  }

  

  public void setNewStatusId(String newStatusId)
  {
    this.newStatusId = newStatusId;
  }


}
